package cn.sky.demo.controller;

import cn.sky.demo.domain.ResponseResult;
import cn.sky.demo.domain.dto.AddCourseDto;
import cn.sky.demo.exception.ValidationGroups;
import cn.sky.demo.repository.Blog;
import cn.sky.demo.repository.BlogRepository;
import cn.sky.demo.utils.ExportWordUtils;
import cn.sky.demo.utils.RedisCache;
import cn.sky.demo.utils.WordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author sky
 * @date 2025年09月16日 16:20
 * @description
 * @since 1.0.0
 */
@Api(value = "课程信息管理接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/test")
public class TestController{
    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private RedisCache redisCache;
    @GetMapping("/get")
    public Object getUser() {
        return redisCache.getCacheObject("user");
    }
    @GetMapping("/set")
    public String setUser(String user) {
        redisCache.setCacheObject("user", user);
        return "success";
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/get1")
    public Object getUser1() {
        return redisTemplate.opsForValue().get("user");
    }
    @GetMapping("/set1")
    public String setUser1(String user) {
        redisTemplate.opsForValue().set("user", user);
        return "success";
    }

    @PostMapping("/course")
    public ResponseResult createCourse(@RequestBody @Validated(value = ValidationGroups.Insert.class) AddCourseDto addCourseDto) {
        return ResponseResult.okResult();
    }
    @PostMapping("/course/update")
    public ResponseResult updateCourse(@RequestBody @Validated(value = ValidationGroups.Update.class) AddCourseDto addCourseDto) {
        return ResponseResult.okResult();
    }
    @GetMapping("PoiWord")
    @ApiOperation(value = "导出word",notes = "导出word")
    public void PoiWord(HttpServletResponse response){
        XWPFDocument document= new XWPFDocument();
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("41");
        try {
            // 这里写你在数据库中查出的数据
            for (String str:list){
                //添加标题
                XWPFParagraph titleParagraph = document.createParagraph();
                XWPFRun titleParagraphRun = titleParagraph.createRun();
                titleParagraphRun.setText("汇报人：苌");

                XWPFParagraph titleParagraph1 = document.createParagraph();
                XWPFRun titleParagraphRun1 = titleParagraph1.createRun();
                titleParagraphRun1.setText("提交状态：按时完成");

                XWPFParagraph titleParagraph2 = document.createParagraph();
                XWPFRun titleParagraphRun2 = titleParagraph2.createRun();
                titleParagraphRun2.setText("汇报内容: ");

                //段落
                XWPFParagraph firstParagraph = document.createParagraph();
                XWPFRun run = firstParagraph.createRun();
                run.setText("安静地被罚款敬爱的师父那订房的哈弗连接吧断开连接你疯啦等会呢弗兰克敬爱的很舒服柯兰多爱上的看法较好的快交付拉山东矿机奋达科技暗示分离爱上的看法建安大口径卡掉数据返回阿斯顿开飞机has");
                run.setColor("000000");//设置字体颜色
                run.setFontSize(10);//设置字体大小
                //换行
                XWPFParagraph paragraph3 = document.createParagraph();
                XWPFRun paragraphRun3 = paragraph3.createRun();
                paragraphRun3.setText("\r");//此处换行符无用 代码实际作用为新增一行插入内容为空
            }
            SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
            String fileName = new String("word导出".getBytes("UTF-8"), "iso-8859-1");
            new WordUtil().exportWord(document,response,fileName+sdf.format(new Date()));
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("导出成功！！！！");
    }

    @ApiOperation(value = "hahah", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping("/demo/export")
    public void export(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        params.put("username","这是标题");
        params.put("substatus","哈哈");
        params.put("subtime","了咯哦");
        params.put("zhouqi","讷讷");
        params.put("neirong","iiik");
        InputStream is = this.getClass().getResourceAsStream("/word/export.docx");
        ExportWordUtils.exportWord(is,"F:/test","aaa.docx",params,request,response);
    }

    @RequestMapping(value = "/params", method=RequestMethod.GET)
    public Page<Blog> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                       @RequestParam(value = "size", defaultValue = "15") Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, sort);
        return blogRepository.findAll(pageable);
    }
    @RequestMapping(value = "/jpalist", method=RequestMethod.GET)
    public Page<Blog> getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
                                                 Pageable pageable) {
        return blogRepository.findAll(pageable);
    }

}
